CSCI E-92: Application Note 7 Using the SysTick timer ----------------------- Rather than attempting to share one timer for more than one purpose (for example, for both the ms-accurate time-of-day clock and for the quantum interrupt), there is an additional timer that I suggest you use. The interface to it is trivial and it is intended to be used to trigger quantum interrupts. This clock is called the SysTick timer and is part of the ARM core implementation and, therefore, is documented in the ARM v7-M Architecture Reference Manual ARM DDI 0403E.b, in section B3.3 "The system timer, SysTick" on pages B3-676 through B3-679 (ARM v7-M Architecture Reference Manual, Errata markup, ARM DDI 0403Derrata 2010_Q3 in section B3.3 "The system timer, SysTick" on pages B3-744 through B3-749). It is a 24-bit, clear-on-write, decrementing, wrap-on-zero counter. We also have a couple of K70 restrictions noted in the K70 Sub-Family Reference Manual, Rev. 4 in section 3.2.1.2 "System Tick Timer" on page 85 (Rev. 2 in section 3.2.1.2 "System Tick Timer" on page 83). This implies that you must select the Processor Clock (using CLKSOURCE in SYST_CSR. Once doing so, when the SysTick is enabled, the SysTick timer will count down once per processor clock cycle. After calling mcgInit(), the processor clock is set to 120 MHz and, as a result, SysTick will be decremented once every 1/120,000,000 seconds, or 8.3333 x 10^(-9), or 8.3333 nanoseconds. Because the largest 24-bit reset value (stored in SYST_RVR) is 2^24, or 16,777,216, that implies that the maximum period for interrupts from the SysTick timer is 16,777,216 x 8.3333 nanoseconds, or 139.81013333 milliseconds. Therefore, it should be clear that this timer supports all reasonable quantum interrupt periods. There are a couple other, more advanced, considerations: (1) The current value of the SysTick timer can be read from SYST_CVR. This would allow your OS to determine how much of the quantum remains after a process might decide to either yield or block. You could use that value to more accurately update the amount of CPU time used by a process. (2) The SysTick timer can be reset to zero (by writing any value into SYST_CVR). This would allow your OS to give each new process a full quantum even if the previous process used only a portion of its quantum. (3) The SysTick timer can be paused. This would allow your OS to give a process a full quantum even if it were interrupted by interrupt service routines. For example, the SysTick timer could be paused when entering an ISR and resumed when leaving an ISR. Beware: you need to be sure that you correctly handle nested ISRs. (4) The priority of SysTick can be altered using the SHPR3 (System Handler Priority Register 3) in section B3.2.12 on page B3-663 of the ARM v7-M Architecture Reference Manual ARM DDI 0403E.b (page B3-724 of the ARM v7-M Architecture Reference Manual, Errata markup, ARM DDI 0403Derrata 2010_Q3 manual). For similar priority setting code, see the svcInit_SetSVCPriority function in svc.c.